package doudizhu;

import java.util.*;

public class Room {
    private List<Card> allcards = new ArrayList<>();
    public Room(){
        // 把54张牌存入
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] colors = {"♠", "♥", "♦", "♣"};

        int size = 0;
        for (String number : numbers) {
            size ++;
            for (String color : colors) {
                Card c = new Card(number, color, size);
                allcards.add(c);
            }
        }

        // 单独导入大小王
        Card c1 = new Card("", "joker",++ size);
        Card c2 = new Card("", "JOKER",++ size);
        Collections.addAll(allcards, c1, c2);

    }

    public void start(){
        // 洗牌
        Collections.shuffle(allcards);

        // 发牌
        List<Card> player1 = new ArrayList<>();
        List<Card> player2 = new ArrayList<>();
        List<Card> player3 = new ArrayList<>();
        int count = 0;
        while (count < 51){
            Card c = allcards.get(count);
            switch (count % 3){
                case 0:
                    player1.add(c);
                    break;
                case 1:
                    player2.add(c);
                    break;
                default:
                    player3.add(c);
                    break;
            }
            count ++;
        }

        // 底牌
        List<Card> lastcards = allcards.subList(51, 54); // 包前不包后

        // 排序
        sortCards(player1);
        sortCards(player2);
        sortCards(player3);

        // 展示牌
        System.out.println("player1:" + player1);
        System.out.println("player2:" + player2);
        System.out.println("player3：" + player3);
        System.out.println("底牌:" + lastcards);

    }

    private void sortCards(List<Card> cards){
        Collections.sort(cards, (o1, o2) ->  o1.getSize() - o2.getSize());
    }
}
